<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use function Matrix\add;    //引入hash

class BlackListRepository {


    /**
     * 新增
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function addRule($param) {
        $addConfig = app('model_allow_rule')->create($param);

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 删除
     */
    public function delRule($idArr = []) {
        $res = app('model_allow_rule')->whereIn("id", $idArr)->delete();

        if ($res) return $res;
        throw new BasicException(93044);
    }

    /**
     * 修改
     */
    public function saveRule($where,$param) {
        $saveConfig = app('model_allow_rule')->where($where)->update($param);

        if ($saveConfig) return $saveConfig;
        throw new BasicException(93047);
    }

    /**
     * 单条
     */
    public function getRule($where) {
        $result = app('model_allow_rule')->where($where)->first();

        return $result;
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getRuleQueryList(Array $params) {
        $whereArr = [];

        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_allow_rule')->where($whereArr)->where($whereFunc)->orderBy('id','desc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }

    /**
     * 所有黑名单类别
     */
    public function getTypeList() {

        $res = app('model_allow_module')->get();

        if (empty($res)) {
            return [];
        }

        $result = array_column($res->toArray(), "cate");

        $result = array_values(array_unique(array_sort($result)));

        return $result;
    }

    /**
     * 新增
     */
    public function addModule($param) {
        $addConfig = app('model_allow_module')->create($param);

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 单条
     */
    public function getModule($where) {
        $result = app('model_allow_module')->where($where)->first();

        return $result;
    }

    /**
     * 删除
     */
    public function delModule($idArr = []) {
        $res = app('model_allow_module')->whereIn("id", $idArr)->delete();

        if ($res) return $res;
        throw new BasicException(93044);
    }

    /**
     * 修改
     */
    public function saveModule($where,$param) {
        $saveConfig = app('model_allow_module')->where($where)->update($param);

        if ($saveConfig) return $saveConfig;
        throw new BasicException(93047);
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getModuleQueryList(Array $params) {
        $whereArr = [];

        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_allow_module')->where($whereArr)->where($whereFunc)->orderBy('id','desc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }


    /**
     * 新增
     */
    public function addUser($param) {
        $addConfig = app('model_allow_user')->create($param);

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 单条
     */
    public function getUser($where) {
        $result = app('model_allow_user')->where($where)->first();

        return $result;
    }

    /**
     * 删除
     */
    public function delUser($idArr = []) {
        $res = app('model_allow_user')->whereIn("id", $idArr)->delete();

        if ($res) return $res;
        throw new BasicException(93044);
    }

    /**
     * 修改
     */
    public function saveUser($where,$param) {
        $saveConfig = app('model_allow_user')->where($where)->update($param);

        if ($saveConfig) return $saveConfig;
        throw new BasicException(93047);
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getUserQueryList(Array $params) {
        $whereArr = [];

        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_allow_user')->where($whereArr)->where($whereFunc)->orderBy('id','desc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }

}